from rest_framework import serializers
from users.models import *


class UserSerializer(serializers.ModelSerializer):
    """用户"""
    class Meta:
        model = User
        fields = ("id", "username", "avatar")


class ArticleSerializer(serializers.ModelSerializer):
    labels = serializers.SerializerMethodField()
    user = serializers.StringRelatedField()

    class Meta:
        model = Article
        fields = "__all__"

    def get_labels(self, obj):
        data = obj.labels.all()
        labels = []
        for i in data:
            labels.append(i.label_name)
        return labels


class QuestionSerializer(serializers.ModelSerializer):
    """发布问题序"""
    labels = serializers.SerializerMethodField()
    user = serializers.StringRelatedField()

    class Meta:
        model = Question
        fields = "__all__"

    def get_labels(self, obj):
        data = obj.labels.all()
        labels = []
        for i in data:
            labels.append(i.label_name)
        return labels


class LabelSerializer(serializers.ModelSerializer):
    """标签序列化器"""
    user = serializers.StringRelatedField()
    questions = serializers.SerializerMethodField(read_only=True)
    articles = serializers.SerializerMethodField()

    class Meta:
        model = Label
        fields = "__all__"

    def get_questions(self, obj):
        data = obj.questions.all()
        return QuestionSerializer(data, many=True).data  # 难点

    def get_articles(self, obj):
        data = obj.articles.all()
        return ArticleSerializer(data, many=True).data  # 难点


class CommentSerializer(serializers.ModelSerializer):
    """问题评论序列化器"""

    user = serializers.StringRelatedField()
    user_id = serializers.IntegerField()

    class Meta:
        model = Comment
        fields = "__all__"


class ReplySerializer(serializers.ModelSerializer):
    """回答问题答案"""
    user = UserSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = "__all__"


class ReplyCommentSerializer(serializers.ModelSerializer):
    """问题评论"""
    user = UserSerializer(read_only=True)
    subs = serializers.SerializerMethodField(read_only=True)  # 自关联的问题ID

    class Meta:
        model = Reply
        fields = ("id",
                  "content",
                  "createtime",
                  "useful_count",
                  "problem",
                  "unuseful_count",
                  "subs",
                  "user",
                  "parent",)

    def get_subs(self, obj):
        """自关联的问题ID"""
        data = obj.subs.all()
        subs = []
        for i in data:
            subs.append(i.content)
        return subs


class ReplyAnswerSerializer(serializers.ModelSerializer):
    """问题答案"""
    user = UserSerializer()
    subs = serializers.SerializerMethodField()  # 自关联的问题ID

    class Meta:
        model = Reply
        fields = ("id",
                  "content",
                  "createtime",
                  "useful_count",
                  "problem",
                  "unuseful_count",
                  "user",
                  "subs",
                  )

    def get_subs(self, obj):
        """自关联的问题ID"""
        data = obj.subs.all()
        subs = ReplySerializer(data, many=True).data
        return subs


class QuestionsSerializer(serializers.ModelSerializer):
    """问题详情"""
    # user = serializers.StringRelatedField()
    user = serializers.CharField()
    # user = UserSerializer(read_only=True)
    labels = serializers.SerializerMethodField()  # 标签列表
    comment_question = serializers.SerializerMethodField()  # 问题评论列表
    answer_question = serializers.SerializerMethodField()  # 问题回答列表

    class Meta:
        model = Question
        fields = (
            "id",
            "createtime",
            "labels",
            "reply",
            "replyname",
            "replytime",
            "title",
            "unuseful_count",
            "useful_count",
            "user",
            "visits",
            "content",
            "comment_question",
            "answer_question"
        )

    def get_labels(self, obj):
        """获取标签queryset"""
        data = obj.labels.all()
        labels = []
        for i in data:
            labels.append(i.label_name)
        return labels

    def get_comment_question(self, obj):
        """评论"""
        comments = obj.replies.filter(type=0)  # type：0:评论问题 1: 评论回答 2: 回答
        comment_question = ReplyCommentSerializer(comments, many=True).data
        return comment_question

    def get_answer_question(self, obj):
        """回答"""
        answers = obj.replies.filter(type=2)
        answer_question = ReplyAnswerSerializer(answers, many=True).data  # 问题答案
        return answer_question
        pass












